<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <!--{include file='comm/header.tpl'}-->
    <title>退款单列表</title>
    <!--{include file='comm/comm_cal.tpl'}-->
    <!--{include file='comm/lodop.tpl'}-->
    <!--{include file="comm/comm_multiselect.tpl"}-->
    <script src="/quanliadmin/docroot/js/createreoprt.js"></script>
</head>
<body>

<div class="container">

    <div class="comm_content">

            <div class="search-box">
                <div style="position:relative">
                    <form id="search_form" class="form-inline" onsubmit="return false;">
                        <table>
                            <tbody>
                            <tr>
                                <td>
                                    <span>退款单号：<input type="text" class="form-control" style="width: 100px;" name="refund_no"></span>
                                    <span>订单编号：<input type="text" class="form-control w150" name="bill_code"></span>
                                    <span>会员名：<input type="text" class="form-control w300" name="custID" size="10"></span>
                                    <span>退货单号：<input type="text" class="form-control" style="width: 100px;" name="return_no" size="10"></span>
                                    <span>
                                        退款来源：<select class="form-control" name="refund_source">
                                            <option value="">请选择</option>
                                            <option value="1">订单退货</option>
                                            <option value="2">订单取消</option>
                                            <option value="3">其他</option>
                                        </select>
                                    </span>
                                     <span>
                                        创建日期：
                                        <input class="form-control w100" id="startDate" name="startDate" value="" readonly="readonly" type="text" />
                                        ～
                                        <input class="form-control w100" id="endDate" name="endDate" value="" readonly="readonly" type="text" />
                                        <button type="button" class="btn btn-default btn-sm" onclick="bill.timeClear();">清 空</button>
                                    </span>
                                     <span>
                                      退款状态：<select class="form-control" name="refund_status">
                                             <option value="">请选择</option>
                                             <option value="1">未审核</option>
                                             <option value="2">已审核</option>
                                             <option value="3">取消</option>
                                             <option value="4">过账</option>
                                         </select>
                                    </span>
                                    <span><input type="button" value="查询" class="btn btn-primary" onclick="bill.search();" /></span>
                                    <span><input type="button" value="重置" class="btn btn-warning" onclick="bill.clear();" /></span>
                                    <span><input  value="预览打印"  onclick="bill.printCheck(1);" class="btn btn-default" type="button"></span>
                                    <span><input  value="直接打印"  onclick="bill.printCheck(2);" class="btn btn-default" type="button"></span>
                                </td>
                            </tr>

                            </tbody>
                        </table>
                    </form>
                </div>
            </div>

            <table id="flex" style="display:none"></table>

        </div>

    

</div>

<div id="pop" style="display: none;">

    <form class="form-horizontal" id="refundForm" onsubmit="return false;">
        <input type="hidden" name="refund_no" id="refund_no" value="">
        <div class="form-group">
            <label for="bill_no" class="col-sm-3 control-label"><span class="red">*</span>订单编号：</label>
            <div class="col-sm-9">
                <input type="text" class="form-control w300" id="return_bill_code" name="return_bill_code" placeholder="请输入订单编号" />
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-3 control-label"><span class="red">*</span> 退款来源：</label>
            <div class="col-sm-9">
                <select class="form-control w150 fl" name="refund_source" id="refund_source" >
                    <option value="">请选择...</option>
                    <option  value="1">订单退货</option>
                    <option  value="2">订单取消</option>
                    <option  value="3">其他</option>
                </select>
            </div>
        </div>

        <div class="form-group">
            <label for="reason_refund" class="col-sm-3 control-label"><span class="red">*</span>   退款原因：</label>
            <div class="col-sm-9">
                <textarea  class="form-control" id="reason_refund" name="reason_refund" style="width:450px;" placeholder="请输入退款原因" ></textarea>
            </div>
        </div>


        <div class="form-group">
            <label for="return_no" class="col-sm-3 control-label"> 退货单号：</label>
            <div class="col-sm-9">
                <input type="text" class="form-control w300" id="return_no" name="return_no" placeholder="请输入退货单号"/>
            </div>
        </div>

        <div class="form-group">
            <label for="payee" class="col-sm-3 control-label"> <span class="red">*</span> 收款人：</label>
            <div class="col-sm-9">
                <input type="text" class="form-control w300" id="payee" name="payee" placeholder="请输入收款人姓名"/>
            </div>
        </div>

        <div class="form-group">
            <label for="refund_account" class="col-sm-3 control-label"> <span class="red">*</span> 退款账号：</label>
            <div class="col-sm-9">
                <input type="text" class="form-control w300" id="refund_account" name="refund_account" placeholder="请输入退款账号" />
            </div>
        </div>

        <div class="form-group">
            <label for="to_city" class="col-sm-3 control-label"> <span class="red">*</span> 银行卡归属城市：</label>
            <div class="col-sm-9">
                <input type="text" class="form-control w300" id="to_city" name="to_city" placeholder="请输入银行卡归属城市" />
            </div>
        </div>

        <div class="form-group">
            <label for="bank_deposit" class="col-sm-3 control-label"> <span class="red">*</span> 开户行：</label>
            <div class="col-sm-9">
                <input type="text" class="form-control w300" id="bank_deposit" name="bank_deposit" placeholder="请输入开户行" />
            </div>
        </div>

        <div class="form-group">
            <label for="refund_amount" class="col-sm-3 control-label"><span class="red">*</span> 退款金额：</label>
            <div class="col-sm-9">
                <input type="text" class="form-control w300" id="refund_amount" name="refund_amount" placeholder="请输入退款金额" />
            </div>
        </div>

        <div class="form-group">
            <label for="remark" class="col-sm-3 control-label"> 备注：</label>
            <div class="col-sm-9">
                <textarea  class="form-control" id="remark" name="remark" style="width:450px;" placeholder="请输入备注信息" ></textarea>
            </div>
        </div>

    </form>

</div>

<div id="pop1" style="display:none">
    <form id="rejected_form" class="form-horizontal" onsubmit="return false;">
        <input type="hidden" id="refundNo" name="refund_no" value="" />
        <div class="form-group">
            <label class="col-sm-2 control-label" for="description">
                驳回原因：
            </label>
            <div class="col-sm-10">
                <textarea style="height:80px;padding-left:10px;" name="reject_cause" id="reject_cause" class="form-control" placeholder="请输入驳回原因"></textarea>
            </div>
        </div>
    </form>
</div>

<script type="text/javascript">
CreateReport("Report");
    $("#flex").flexigrid({
        url:"/invokingAjax-backend/bill/billRefund.ajax?operFlg=1",
        dataType: "json",
        colModel : [
            {display: "操作", width : 120, align: "center"},
            {display: "状态", width : 70, align: "center"},
            {display: "退款单号", name : "refund_no", width : 100, align : "center" ,sortable : true},
            {display: "订单编号", width : 200,align : "center"},
            {display: "用户名", width : 100},
            {display: "退货单号", width : 80},
            {display: "打印次数", width : 80},
            {display: "支付方式", width : 90},
            {display: "退款账户", width : 170},
            {display: "收款人", width : 80},
            {display: "退款金额", width : 80},
            {display: "退款来源", width : 100},
            {display: "退款原因", width : 100},
            {display: "银行卡归属城市", width : 100},
            {display: "开户行", width : 100}
        ],

        buttons : [
            <!--{if $edit_permission  && $posted_permission}-->
            {name: "添加", bclass: "add", onpress : "bill.add"},
            {name: "批量过账", bclass: "batch_posting", onpress : "bill.batchPosting"},
            {name: "全选", bclass: "checkAll", onpress : "E.checkAllList"},
            {separator: true},
            {name: "取消全选", bclass: "cancelCheckAll", onpress : "E.cancelCheckAllList"}
             <!--{elseif $edit_permission}-->
            {name: "添加", bclass: "add", onpress : "bill.add"}
             <!--{elseif  $posted_permission}-->
            {name: "批量过账", bclass: "batch_posting", onpress : "bill.batchPosting"},
            {name: "全选", bclass: "checkAll", onpress : "E.checkAllList"},
            {separator: true},
            {name: "取消全选", bclass: "cancelCheckAll", onpress : "E.cancelCheckAllList"}
            <!--{/if}-->
        ],



        sortname: "refund_no",
        sortorder: "desc",
        usepager: true,
        rp: 10,
        useRp: true,
        showTableToggleBtn: true,
        autoload: true,
        onSubmit: function() {
            var dt = $('#search_form').serializeArray();
            $("#flex").flexOptions({params: dt});
            return true;
        }
    });

    $(function() {

        $(document).on('blur', '#return_bill_code', function() {

            var bill_code = E.trim($(this).val());

            if (E.isEmpty(bill_code)) {
                return false;
            }

            E.ajax_get({
                url: '/invokingAjax-backend/bill/billSearch.ajax?operFlg=2',
                data: {
                    bill_code: bill_code
                },
                call: function( o ) {
                    if (o.code == 200) {

                    } else {
                        E.alert(o.message, 1, function() {
                            $('#return_bill_code').focus();
                        });
                    }
                }
            });

        });

    });

    var bill = {

        search: function() {
            var dt = $("#search_form").serializeArray();
            $("#flex").flexOptions({params: dt}).flexReload();
        },

        //重置
        clear:function(){
            $(".form-control").val('');
            this.search();
        },

        billview: function( bill_no ) {
            window.open('/quanliadmin/app/bill/billSearch.pscript?operFlg=9&bill_no=' + bill_no);
        },

        returnview: {

                open: function( return_no, operFlg ) {

                    bill.return_no = return_no;
                    this.operFlg = operFlg;

                    E.ajax_get({
                        url: '/invokingAjax-backend/bill/billReturn.ajax?operFlg=7&xjx=none',
                        data: {
                            return_no: return_no
                        },
                        call: 'bill.returnview.result'
                    });

                },

                result: function( o ) {

                    if (o.code == 200) {

                        var return_obj = o.data;

                        var html = '<div class="panel panel-default form-horizontal">';
                        html += '<div class="panel-heading">退货单信息</div>';
                        html += '<div class="panel-body">';

                        html += '<div class="form-group">';
                        html += '<label class="col-sm-2 control-label">退货单编号</label>';
                        html += '<div class="col-sm-10">';
                        html += '<p class="form-control-static">' + return_obj.return_no + '</p>';
                        html += '</div>';
                        html += '</div>';

                        html += '<div class="form-group">';
                        html += '<label class="col-sm-2 control-label">订单号</label>';
                        html += '<div class="col-sm-10">';
                        html += '<p class="form-control-static">' + return_obj.bill_code + '</p>';
                        html += '</div>';
                        html += '</div>';

                        if (return_obj.return_reason_2 == null || return_obj.return_reason_2 == 'null') {
                            return_obj.return_reason_2 = '';
                        }

                        html += '<div class="form-group">';
                        html += '<label class="col-sm-2 control-label">退换货原因</label>';
                        html += '<div class="col-sm-3">';
                        html += '<p class="form-control-static">' + return_obj.return_reason_1 + '</p>';
                        html += '</div>';
                        html += '<label class="col-sm-1 control-label">内容</label>';
                        html += '<div class="col-sm-4">';
                        html += '<p class="form-control-static">' +  return_obj.return_reason_2 + '</p>';
                        html += '</div>';
                        html += '</div>';

                        html += '<div class="form-group">';
                        html += '<label class="col-sm-2 control-label">问题描述</label>';
                        html += '<div class="col-sm-10">';
                        html += '<p class="form-control-static">' + return_obj.return_memo + '</p>';
                        html += '</div>';
                        html += '</div>';

                        html += '<div class="form-group">';
                        html += '<label class="col-sm-2 control-label">商品返回方式</label>';
                        html += '<div class="col-sm-10">';
                        html += '<p class="form-control-static">' + return_obj.goods_return_type_name + '</p>';
                        html += '</div>';
                        html += '</div>';

                        if (return_obj.goods_return_type == 2) {
                            var addr = return_obj.pick_province + return_obj.pick_city + return_obj.pick_county + return_obj.pick_address;
                            html += '<div class="form-group">';
                            html += '<label class="col-sm-2 control-label">取货地址</label>';
                            html += '<div class="col-sm-10">';
                            html += '<p class="form-control-static">' + addr + '</p>';
                            html += '</div>';
                            html += '</div>';
                        }

                        html += '<div class="form-group">';
                        html += '<label class="col-sm-2 control-label">联系人姓名</label>';
                        html += '<div class="col-sm-10">';
                        html += '<p class="form-control-static">' + return_obj.custName + '</p>';
                        html += '</div>';
                        html += '</div>';

                        html += '<div class="form-group">';
                        html += '<label class="col-sm-2 control-label">手机号码</label>';
                        html += '<div class="col-sm-10">';
                        html += '<p class="form-control-static">' + return_obj.mobile + '</p>';
                        html += '</div>';
                        html += '</div>';

                        html += '</div>';
                        html += '</div>';

                        html += '<div class="panel panel-default">';
                        html += '<div class="panel-heading">退货商品</div>';
                        html += '<table class="table">';
                        html += '<thead>';
                        html += '<tr>';
                        html += '<th width="180">商品编号</th>';
                        html += '<th width="*">商品名称</th>';
                        html += '<th width="220">规格</th>';
                        html += '<th width="80">退货数量</th>';
                        html += '</tr>';
                        html += '</thead>';
                        html += '<tbody>';
                        $.each(return_obj.return_goods, function(k, v) {
                            html += '<tr>';
                            html += '<td>' + v.goodsID + '</td>';
                            html += '<td>' + v.goodsName + '</td>';
                            html += '<td>' + v.spec + '</td>';
                            html += '<td class="tac">' + v.goods_amount + '</td>';
                            html += '</tr>';
                        });
                        html += '</tbody>';
                        html += '</table>';
                        html += '</div>';

                            E.popup.open({
                                title: '退货单详情',
                                content: html,
                                css: 'width: 800px;'
                            });

                    } else {
                        E.alert(o.message);
                    }

                }
        },

        audit: function( refund_no , refund ) {
            E.open("/quanliadmin/app/bill/billRefund.pscript?operFlg=100&refund_no="+refund_no+"&refund="+refund,'',{height: 600, width: 1024 ,top : 10 ,left : 150});
        },

        //过账
        posting: function( refund_no ){
            this.refund_no = refund_no;
            E.confirm("您是否确认过账？","bill.confirmPosting");
        },

        confirmPosting: function(){
            var dt = {
                refund_status : 4,
                refund_no : this.refund_no
            };
            E.ajax_post({
                url: "/invokingAjax-backend/bill/billRefund.ajax?operFlg=103",
                data: {
                    data: dt
                },
                call: "bill.result"
            });
        },

        //驳回
        reject: function( refund_no ){
            this.refund_no = refund_no  ;
            //调用打开弹出层公共JS方法
            E.popup.open({
                id:"pop1",   		 //获取id为"pop"元素内的HTML
                title: "退款单驳回",       //弹出层标题
                css: "width:800px;",     //设置弹出层样式
                btnName: "确定驳回",         //执行“确定”操作的按钮名称
                btnCall:"bill.turnDown();"  //点击确定后执行的方法
            });
        },
        turnDown: function(){
            $("#refundNo").val(this.refund_no);
            this.dt = E.getFormValues("rejected_form");
            E.loadding.open("正在驳回，请稍后...");
            E.ajax_post({
                url: "/invokingAjax-backend/bill/billRefund.ajax?operFlg=104",
                data: this.dt,
                call: "bill.result"
            });
        },

        result: function( o ) {

            E.loadding.close();

            if (o.code == 200) {
                E.popup.close();
                E.alert(o.message, 2);
                bill.search();
            } else {
                E.alert(o.message);
            }
        },

        //取消
        cancel : function (refund_no){
            this.refund_no = refund_no;
            E.confirm("您是否确认取消？","bill.confirmCancel");
        },

        confirmCancel : function (){

            var dt = {
                refund_status : 3,
                refund_no : this.refund_no
            };

            E.ajax_post({
                url: "/invokingAjax-backend/bill/billRefund.ajax?operFlg=101",
                data: {
                    data: dt
                },
                call: "bill.result"
            });
        },

        reload : function (){
            $("#flex").flexReload();
        },

        //新增退款单
        add: function(refund_no){
            if(refund_no){
                 var url = '/invokingAjax-backend/bill/billRefund.ajax?operFlg=2&refund_no='+refund_no;
            }else{
                var url = '/invokingAjax-backend/bill/billRefund.ajax?operFlg=2';
            }

            E.loadding.open('正在操作，请稍候...');
            E.ajax_post({
                url: url,
                //data: {},
                call: "bill.open"
            });

        },
        open: function(o){

            E.loadding.close();

            var title = "退款单修改";

            if(o.code == 400){
                title = "退款单新增";
                this.addBox = 'add_user';
            }
            E.popup.open({
                id:"pop",   		 //获取id为"pop"元素内的HTML
                title: title,       //弹出层标题
                css: "width:800px;",     //设置弹出层样式
                btnName: "保存",         //执行“确定”操作的按钮名称
                btnCall:"bill.check();"  //点击确定后执行的方法
            });

            if(o.code == 200){

                $('#refund_no').val(o.data.refund_no);
                $('#return_bill_code').val(o.data.bill_code);
                $('#refund_source').val(o.data.refund_source);
                $('#return_no').val(o.data.return_no);
                $('#payType').val(o.data.payTypeID+'_'+o.data.payTypeName);
                $('#payee').val(o.data.payee);
                $('#refund_account').val(o.data.refund_account);
                $('#refund_amount').val(o.data.refund_amount);
                $('#remark').val(o.data.remark);
                $('#bank_deposit').val(o.data.bank_deposit);
                $('#to_city').val(o.data.to_city);
                $('#reason_refund').val(o.data.reason_refund);

            }
        },

        //验证退款单
        check: function(){

            this.dt = E.getFormValues("refundForm");
            var refund_source =  $("#refund_source").val();//退款来源
            var return_no = $.trim( $("#return_no").val() );//退货单号
            var refund_amount = $.trim( $("#refund_amount").val() );//退款金额
            var reason_refund = $.trim($("#reason_refund").val());//退款原因
            var payee=$.trim($("#payee").val());//收款人
            var refund_account=$.trim($("#refund_account").val());//退款账号
            var to_city=$.trim($("#to_city").val());//银行卡归属城市
            var bank_deposit=$.trim($("#bank_deposit").val());//开户行

            var err_msg = '';

            //订单编号
            if($("#return_bill_code").val() == ''){
                err_msg += '请输入订单编号<br />';
            }

            //检查退款来源
            if(refund_source == ''){
                err_msg += '请选择退款来源<br />';
            }

            //如有订单号检查订单号
            if(return_no != '' && !E.isDigital(return_no)){
                err_msg += '请输入正确的退货单号<br />';
            }

            //检查退款金额
            if(!E.isMoney(refund_amount)){
                err_msg += '请输入正确的退款金额<br />';
            }

            //检查退款原因
            if(E.isEmpty(reason_refund)){
                err_msg += '请输入退款原因<br />';
            }

            //检查收款人
            if(E.isEmpty(payee)){
                err_msg += '请输入收款人<br />';
            }

            //检查退款账号
            if(E.isEmpty(refund_account)){
                err_msg += '请输入退款账号<br />';
            }

            //检查银行卡归属城市
            if(E.isEmpty(to_city)){
                err_msg += '请输入银行卡归属城市<br />';
            }

            //检查开户行
            if(E.isEmpty(bank_deposit)){
                err_msg += '请输入开户行<br />';
            }

            if(err_msg != ''){
                E.alert(err_msg);
            }else{
                bill.execute();
            }

        },

        //新增退款单执行
        execute: function(){
            E.loadding.open("正在保存，请稍候...");
            E.ajax_post({
                url: "/invokingAjax-backend/bill/billRefund.ajax?operFlg=100",
                data: this.dt,
                call: "bill.results"
            });
        },

        //返回结果
        results:function(o){
            E.loadding.close();
            if (o.code != 200) {
                E.alert(o.message);
            } else {
                E.popup.close();
                E.alert("保存成功", 2, "bill.reload");
            }
        },
        //清空日期
        timeClear:function(){
            $('#startDate').val('');
            $('#endDate').val('');
        },
        //批量过账
        batchPosting:function(){
            //获取选中的行的行数
            var selected_count = $('.trSelected',flex).length;
            if (selected_count==0) {
                E.alert("请选择要过账的的数据");
                return;
            }
            E.confirm("您是否确认过账？","bill.checkBatchPosting");
        },
        checkBatchPosting:function(){
            var selected_count = $('.trSelected',flex).length;

            //打印的数据行订单号
            var tempArr = new Array();
            for(var i=0; i<selected_count; i++){
                var templateIDStr = $('.trSelected',flex).children('.sorted').eq(i).text();
                tempArr.push(templateIDStr);
        }
            E.ajax_post({
                url: "/invokingAjax-backend/bill/billRefund.ajax?operFlg=105",
                data: {
                    billNoArr:tempArr
                },
                call: 'bill.refresh'
            });

    },

        refresh: function( o ) {

            E.loadding.close();

            if (o.code == 200) {
                E.alert(o.message, 2);
                bill.search();
            } else {
                E.alert(o.message);
                bill.reload();
            }
        },

      //打印退款单
        //获取打印的行数
        printCheck:function( para ){
            //获取选中的行的行数
            bill.para = para; //打印参数 区分打印预览 与 直接打印
            var selected_count = $('.trSelected',flex).length;
            if (selected_count==0) {
                E.alert("请选择要打印的数据");
                return;
            }

            var tempArr = new Array(); //打印的数据行的退款单号
            for(var i=0; i<selected_count; i++){
                var templateIDStr = $('.trSelected',flex).eq(i).children('td').eq(2).text();
                tempArr.push(templateIDStr);
            }
            Report.LoadFromURL('/report/BILL_REFUND.grf?b='+Math.floor(Math.random() * ( 1000 + 1)));
            $.each(tempArr, function(k, v) {
                Report.LoadDataFromURL('/report/print_refund.php?id=0&refund_no=' + v);
                var sub_GoodsArray = Report.ControlByName("SubReport_GoodsArray").AsSubReport.Report;
                sub_GoodsArray.OnInitialize = function(){
                    //载入子报表数据
                    sub_GoodsArray.LoadDataFromURL('/report/print_refund.php?id=1&refund_no=' + v);
                };
                var SubReport_AgentGoodsArray = Report.ControlByName("SubReport_AgentGoodsArray").AsSubReport.Report;
                SubReport_AgentGoodsArray.OnInitialize = function(){
                    //载入子报表数据
                    SubReport_AgentGoodsArray.LoadDataFromURL('/report/print_refund.php?id=2&refund_no=' + v);
                };
                var SubReport_Logs = Report.ControlByName("SubReport_Logs").AsSubReport.Report;
                SubReport_Logs.OnInitialize = function(){
                    //载入子报表数据
                    SubReport_Logs.LoadDataFromURL('/report/print_refund.php?id=3&refund_no=' + v);
                };
                var SubReport_posting = Report.ControlByName("Posting_information").AsSubReport.Report;
                SubReport_posting.OnInitialize = function(){
                    //载入子报表数据
                    SubReport_posting.LoadDataFromURL('/report/print_refund.php?id=4&refund_no=' + v);
                };

               if (para == 1) { //预览
                    Report.PrintPreview(true);
                } else { //直接打印
                   Report.Print(false);
                }
            });

            if( para == 2 ){ //更改打印次数
                E.ajax_post({
                    url: "/invokingAjax-backend/bill/billRefund.ajax?operFlg=106",
                    data: {
                        printBillNoArr:tempArr
                    },
                    call: 'bill.callRes'
                });
            }

        },
        //打印成功回调
        callRes:function(o){
            if(o.code != 200){
                E.alert( o.message );
            }else{
                E.alert('打印成功',2);
                bill.load();
            }
        },
        //重新加载
        load: function() {
            $("#flex").flexReload();
        }



    };
    //日期
    $('#startDate').datepicker();
    $('#endDate').datepicker();
</script>

</body>
</html>